
var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

layer.ready(function () {
    //获取所有用户信息
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $("#salesmanCode").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                }
                form.render('select');
            }
        }
    });
});
let initData = [];
table.render({
    elem: '#mains'
    ,data:initData
    ,url:"payment/getImportCarryDetail"
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'invoiceNumber', title:'发票编号',sort:true}
        ,{field:'contractNo', title:'外销合同编号',sort:true}
        ,{field:'purchaseNo', width: 120, title: '采购合同号', sort: true}
        ,{field:'supplierName', title:'供应商名称',sort:true}
        ,{field:'salesmanName', title:'业务员',sort:true}
        ,{field:'operator', title:'操作员',sort:true}
        ,{field:'amountsPayable', title:'应付总金额',sort:true}
        ,{field:'unPaidAmount', title:'未付款金额',sort:true}
        ,{field:'totalApplication', title:'已申请金额',sort:true}
        ,{field:'earnestRate', title:'采购付款比例',sort:true}
        ,{field:'amountsPayable', title:'明细比例金额',sort:true}
        ,{field:'companyName', title:'制单公司',sort:true}
        ,{field:'currency', title:'币种',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 600
});

form.on('submit(submitform)', function(data){
    table.reload('mainstableid', {
        where: data.field
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    let productData =   table.checkStatus("mainstableid").data;
    if(productData.length == 0){
        layer.msg("请选择数据")
    }else{
        //获取父页面明细数据
        let oldData =  parent.table.cache["maininformationtableid"];
        let num = oldData.length+1;
        $.each(productData, function (index, value) {
            //添加临时id
            productData[index]["tempId"] = GenNonDuplicateID(3) + num;
            //登记货款
            productData[index]["type"] = 2;
            productData[index]["berPersonName"] = productData[index]["salesmanName"];
            productData[index]["berPersonCode"] = productData[index]["salesmanCode"];
            productData[index]["signedCompanyCode"] = productData[index]["companyCode"];
            productData[index]["signedCompanyName"] = productData[index]["companyName"];
            productData[index]["allocatedAmount"] = 0;

            num++;
            //添加数据
            oldData.push(productData[index]);
        });
        parent.table.reload('maininformationtableid',{data : oldData, limit: oldData.length});
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})

//生成临时id
function GenNonDuplicateID(randomLength){
    return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}